package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/Request2")
public class RequestController2 {
    //传递JSON
    @RequestMapping("/r1")
    public String r1(@RequestBody uesrInfo uesrinfo){
        return uesrinfo.toString();
    }
    @RequestMapping("/article/{type}/{articleId}")
    public String r2(@PathVariable String type,@PathVariable Long articleId){
        return "articleId:"+articleId+",type:"+type;
    }
    //上传文件
    @RequestMapping("/r3")
    public String r3(@RequestPart("file") MultipartFile file){
        System.out.println(file.getOriginalFilename());
        return "文件显示成功";
    }
    @RequestMapping("/r4")
    public String r4(HttpServletRequest request){
        Cookie[] cookie=request.getCookies();
        if(cookie!=null){
            for(int i=0;i<cookie.length;i++){
                System.out.println(cookie[i].getName()+":"+cookie[i].getValue());
            }
        }
        return "返回cookie成功";
    }
    @RequestMapping("/r5")
    public String r4(@CookieValue("java")String java){
        return "从Cookie中获取java的值"+java;
    }
    //储存session
    @RequestMapping("/setsession")
    public String setsession(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("usertname","杨森");
        session.setAttribute("age",20);
        return "session设置成功";
    }
    //获取session
    @RequestMapping("/getsession")
    public String getsession(HttpServletRequest request){
        HttpSession session=request.getSession(false);//如果用户登录，session有值，未登录，为null
        if(session==null){
            return "用户未登录";
        }else{
            String username= (String)session.getAttribute("usertname");
            int age= (int)session.getAttribute("age");
            return "登录用户为"+username+":"+age;
        }
    }
    //获取session
    @RequestMapping("/getsession2")
    public String getsession2(HttpSession session){
      String username=(String) session.getAttribute("usertname");
        return "登录用户名 "+username;
    }
    @RequestMapping("/getsession3")
    public String getsession3(@SessionAttribute("usertname")String usertname){
        return "登录用户名 "+usertname;
    }
    @RequestMapping("/getheader")
    public String getheader(HttpServletRequest request){
        String usetAgent=request.getHeader("User-Agent");
        return "Uset-Agent"+usetAgent;
    }
    @RequestMapping("/getheader2")
    public String getheader2(@RequestHeader("User-Agent")String usetAgent){
        return "Uset-Agent2"+usetAgent;
    }
    //对象转json
    @RequestMapping("/r6")
    public String r5(){
        uesrInfo user =new uesrInfo("杨森",20);
        return user.toString();
    }
    //修改状态码
    @RequestMapping("/r7")
    public String r7(HttpServletResponse response){
        response.setStatus(404);
        return "修改状态码";
    }
    @RequestMapping("r8")
    public String r8(HttpServletResponse response){
        response.setHeader("yangsen","yangsen");
        return "修改header";
    }
}
